//
//  WeatherView.m
//  QuickCure1
//
//  Created by Ashzenki on 01/11/2016.
//  Copyright © 2016 maoge. All rights reserved.
//

#import "WeatherView.h"
#import "HomeWeatherModel.h"

@interface WeatherView ()<CAAnimationDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *iv_weather;
@property (weak, nonatomic) IBOutlet UILabel *lb_weather;
@property (weak, nonatomic) IBOutlet UILabel *lb_temperature;
@property (weak, nonatomic) IBOutlet UILabel *lb_date;
@property (weak, nonatomic) IBOutlet UILabel *lb_wind;
@property (weak, nonatomic) IBOutlet UIImageView *iv_backGroundImageV;
@property (weak, nonatomic) IBOutlet UILabel *lb_timeTempreture;

@property (weak, nonatomic) IBOutlet UILabel *lb_haveCold;
@property (weak, nonatomic) IBOutlet UILabel *lb_isHaveCold;
@property (weak, nonatomic) IBOutlet UILabel *lb_haveColdDesc;


@end

@implementation WeatherView

+ (instancetype)weatherView {
    
    return [[NSBundle mainBundle] loadNibNamed:@"WeatherView" owner:nil options:nil].lastObject;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
}


#pragma mark - 太阳旋转
- (void)moveSun {
    
    //动画
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    anim.duration = 10;
    anim.toValue = [NSNumber numberWithFloat:2 * M_PI];
    anim.repeatCount = INT32_MAX;
    
    [_iv_weather.layer addAnimation:anim forKey:nil];
}

- (void)moveCloud {
    
    CAKeyframeAnimation *keyAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    keyAnim.duration = 4;
    keyAnim.repeatCount = INT32_MAX;
    
    //获取当前位置
    CGPoint currentPoint = _iv_weather.layer.position;
    
    NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(currentPoint.x, currentPoint.y)];
    NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(currentPoint.x, currentPoint.y - 3)];
    NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(currentPoint.x, currentPoint.y + 3)];
    NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(currentPoint.x, currentPoint.y)];
    
    keyAnim.values = @[value1, value2, value3, value4];
    
    [_iv_weather.layer addAnimation:keyAnim forKey:nil];
    
}




#pragma mark - 地点切换
- (void)setupLocationWithCity:(NSString *)cityName {
    
    //获取字体内容
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", cityName]];
    
    //设置下划线范围
    NSRange contentRange = {0, [content length]};
    
    //加下划线
    [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
    
    _bt_location.titleLabel.attributedText = content;
}



#pragma mark - 地点被点击
- (IBAction)clickButton {
    
    //字体颜色变蓝
    [_bt_location setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"clickLocation" object:nil];
}



#pragma mark - 获取天气网络数据

- (void)getWeatherDataWithCity:(NSString *)cityName {
    
    HomeWeatherModel *model = [[HomeWeatherModel alloc] init];
    
    [model getWeatherDataWithCity:cityName complete:^(HomeWeatherModel *model, NSError *error) {
        

        if(model.zs){
            
            _lb_haveCold.font = MYROUNDFONT(17);
            
            _lb_isHaveCold.text = model.zs;
            _lb_isHaveCold.font = MYROUNDFONT(17);
            
            _lb_haveColdDesc.text = model.des;
            _lb_haveColdDesc.font = MYROUNDFONT(17);
            
            //地点
            _bt_location.titleLabel.font = MYROUNDFONT(26);
            
            
            //天气
            _lb_wind.font = MYROUNDFONT(17);
            _lb_wind.text = model.wind;
            
            
            //温度
            NSArray *tempArr = [model.temperature componentsSeparatedByString:@" "];
            NSArray *tempArr2 = [tempArr[2] componentsSeparatedByString:@"℃"];
            
            NSString *celsius = @"°";
            NSString *gang = @"/";
            
            _lb_temperature.font = MYROUNDFONT(17);
            _lb_temperature.text = [NSString stringWithFormat:@"%@%@%@%@%@", tempArr2[0], celsius, gang, tempArr[0], celsius];
            
            
            //日期
            NSArray *dateArr = [model.date componentsSeparatedByString:@" "];
            
            _lb_date.font = MYROUNDFONT(17);
            _lb_date.text = [NSString stringWithFormat:@"%@ %@", dateArr[0], dateArr[1]];
            
            //得到实时气温
            NSString *timeTemprature = dateArr.lastObject;
            
            NSArray *timeTemArr = [timeTemprature componentsSeparatedByString:@"："];
            
            NSString *timeTemStr = [timeTemArr.lastObject stringByReplacingOccurrencesOfString:@")" withString:@""];
            
            
            _lb_timeTempreture.font = MYROUNDFONT(28);
            _lb_timeTempreture.text = timeTemStr;
            
            
            //天气状况
            _lb_weather.font = MYROUNDFONT(17);
            _lb_weather.text = model.weather;
            
        }
        else {
            
            _lb_haveCold.font = MYROUNDFONT(17);
            _lb_haveCold.text = @"";
            
            _lb_isHaveCold.text = @"";
            _lb_isHaveCold.font = MYROUNDFONT(17);
            
            _lb_haveColdDesc.text = @"网络不给力~(≧▽≦)/~";
            _lb_haveColdDesc.font = MYROUNDFONT(17);
        }
        
        //改变背景图片
        if ([model.weather isEqualToString:@"晴"]) {
            
            _iv_weather.image = [UIImage imageNamed:@"晴"];
            
            _iv_backGroundImageV.image = [UIImage imageNamed:@"intelAssist_weather_background_sunny"];
            
            [self moveSun];
        }
        else if ([model.weather containsString:@"霾"] || [model.weather containsString:@"阴"]) {
            
            _iv_weather.image = [UIImage imageNamed:@"多云"];
            
            _iv_backGroundImageV.image = [UIImage imageNamed:@"intelAssist_weather_background_smog"];
            
            [self moveCloud];
        }
        else if ([model.weather containsString:@"云"]) {
            
            _iv_weather.image = [UIImage imageNamed:@"多云转晴"];
            
            _iv_backGroundImageV.image = [UIImage imageNamed:@"intelAssist_weather_background_sunny"];
            
            [self moveCloud];
        }
        else if([model.weather containsString:@"雪"]){
            
            _iv_weather.image = [UIImage imageNamed:@"雪"];
            
            _iv_backGroundImageV.image = [UIImage imageNamed:@"intelAssist_weather_background_snowy"];
            
            [self moveCloud];
            
        }else if ([model.weather containsString:@"雨"]){
            
            _iv_weather.image = [UIImage imageNamed:@"下雨"];
            
            _iv_backGroundImageV.image = [UIImage imageNamed:@"intelAssist_weather_background_rainy"];
            
            [self moveCloud];
        }
        
    }];
}

@end


































